Activity 3 - Operators

  1. Arithmetic Operations:

    1. Write a PHP script to calculate the area of a rectangle using arithmetic operators. Display the result. (area of a rectangle: $area = $length * $width; )
    2. Implement a script that converts currency from one denomination to another using arithmetic operators. Display the converted amount. (Convert currency: $converted_amount = $amount * $conversion_rate;)

      A. Solution

          $length = 2;
          $width = 3;
          $area = $length * $width
          echo "Convert currency: $area";
      

      Output

      Area of rectangle: 6

      B. Solution

          $amount = 50;
          $conversion_rate = 56;
          $converted_amount = $amount * $conversion_rate;
          echo "Convert currency: $converted_amount";
      

      Output

      Convert currency: 2800
  2. Assignment Operators:

    1. Track the progress of a fundraising campaign by updating the donation total dynamically using combined assignment operators. Display the updated total. (Track fundraising: $total_donation += $new_donation;)
    2. Implement a voting system where candidate votes are incremented based on user input. Display the final vote count for each candidate. (Voting system: $candidate_votes += $user_vote;)
    3. A. Solution

          $total_donation = 500;
          $new_donation = 1000;
          $total_donation += $new_donation
          echo "Track fund raising: $total_donation";
      

      Output

      Track fund raising: 1500

      B. Solution

          $candidate_votes = 100;
          $user_vote = 50;
          $candidate_votes += $user_vote;
          echo "Voting System: $candidate_votes";
      

      Output

      Voting System: 150
  3. Comparison Operators:

    1. Compare the performance of two athletes in a race and determine the winner using comparison operators. Display the result. (Compare athlete times: Use '<' to compare times.)
    2. Evaluate the efficiency of two algorithms based on their execution times using comparison operators. Display the more efficient algorithm. (Evaluate algorithm efficiency: Use '<' to compare times.)
    3. A. Solution

          $athletes1= 50;
          $athletes2 = 100;
          $winner = $athletes1 < $athletes2
          echo "the winner is: $winner";
      

      Output

      the winner is: 1

      B. Solution

          $algorithms1 = 100;
          $algorithms2  = 50;
          $winner =  $algorithms1 < $algorithms2 ;
          echo "Evaluate algorithm efficiency: $winner";
      

      Output

      faster algorithm efficiency: 1
  4. Increment/Decrement Operations:

    1. Calculate the factorial of a number using postfix increment/decrement operations. Display the factorial. (Calculate factorial: Use a loop to multiply numbers.)
    2. Simulate the movement of a vehicle along a track using prefix increment/decrement operations. Display the final position of the vehicle. (Simulate vehicle movement: Use += for forward movement.)
    3. A. Solution

      $num = 10;
      $factorial = 1;
      for ($i = 1; $i <= $n; $i++) {
       $factorial *= $i;
      }
      echo "Calculate factorial of $num: $factorial";
      

      Output

      Calculate factorial of 10 : 3628800

      B. Solution

      $position = 10;
      $distance = 10;
      $position += $distance;
      echo "vehicle movement:e: $position";
      

      Output

      Vehicle movement: 20
  5. Logical Operators:

    1. Determine eligibility for a discount based on purchase amount and customer loyalty using logical operators. Display whether the customer is eligible for a discount. (Determine discount eligibility: Use && to check conditions.)
    2. Design a decision-making system for a chatbot to respond to user queries using logical operators. Display the appropriate response based on the query. (Chatbot decision-making: Use ? : for simple conditions.)
    3. A. Solution

      $purchaseAmount = 100;
      $loyaltyPoints = 50;
      $minPurchaseAmount = 50;
      $minLoyaltyPoints = 40;
      
      $isEligibleForDiscount = ($purchaseAmount >= $minPurchaseAmount) && ($loyaltyPoints >= $minLoyaltyPoints);
      echo "Determine discount 
      eligibility: $isEligibleForDiscount";
      

      Output

      Discount eligibility: Eligible

      B. Solution

      $user_query = "Good Morning";
      $response = ($user_query == "weather") ? "Good Morning " : "Bad Morning";
      echo "Chatbot response: $response";
      

      Output

      Chatbot desicion-making: Good Morning! :)